| Simple | |
|
T o o l s -Assembler --Script examples ---Simple ---Loops ---Mini arithmetic compiler |
|
This
program generates a simple class that does absolutely
nothing besides getting initialized. There are basically two things
you need to do to make a class file, specify the hierarchy where the
class belongs, and write the methods for the class.
(define my-class (make-class-env)) (jas-class-setsuperclass my-class (make-class-cpe "java/lang/Object")) (jas-class-setclass my-class (make-class-cpe "out")) make-class-env creates an instance of a class-env,
which is a container object used to used to store information about the
contents of a class. As various pieces of a class become available,
they are added through the functions that are available to
manipulate class-env objects.
The
You can keep creating new cpe items and adding them to the class-env. The package will internally "uniquefy" cpe items, so at the end only one unique copy will be generated in the class file. You can reuse cpe items too, if you wish. It is sometimes just more convenient to create them on the fly instead of carting them with you all the time. The second step is defining the methods in the class. We will define only one method, the initializer for the class, and just make it call the constructor of its super class. We'll divide this into two steps, first creating the code for the method, and then specifying the access to the method and arguments, etc.
(define init-code (make-code))
(jas-code-addinsn init-code
(aload_0))
(jas-code-addinsn init-code
(invokenonvirtual
(make-method-cpe "java/lang/Object" "<init>" "()V")))
(jas-code-addinsn init-code
(return))
make-code is a function that creates a new instance of
a code-body. code-bodies are strictly non-pornographic and represent
the body of a method. Once you create a code-body, you can append
instructions to it with the jas-code-addinsn function,
Other methods exist to add a catch table to the body to
handle exceptions.The second step in defining methods is to specify arguments and access control to the method.
(jas-class-addmethod my-class acc-public "<init>" "()V" init-code ())The jas-class-addmethod
function allows a code body to be added to a class. It takes a few
parameters which describe the access permissions to the class, its name,
signature and any exception attributes for the method.
At this point, the code to generate the bytecode is ready to be
generated, and the entire program looks like this.
;; script to create a class that does nothing.
; make-class-env creates
; a ClassEnv object which is
; used to store information about
; an object.
(define my-class (make-class-env))
; make-code creates a code object
; which contains the body of a
; method.
(define init-code (make-code))
(jas-code-addinsn init-code
(aload_0))
(jas-code-addinsn init-code
(invokenonvirtual
(make-method-cpe "java/lang/Object" "<init>" "()V")))
(jas-code-addinsn init-code
(return))
; fill up the class with goodies
(jas-class-setsuperclass my-class (make-class-cpe "java/lang/Object"))
(jas-class-setclass my-class (make-class-cpe "out"))
(jas-class-setaccess my-class acc-public)
(jas-class-addmethod my-class acc-public "<init>" "()V" init-code ())
; and write it all out
(jas-class-write my-class (make-outputstream "out.class"))
This example is provided with the distribution, and this sequence
shows how to use the script driver to run this piece of code
from the distribution root directory.
% java scm.driver examples/simple.jas
% ls -l out.class
-rw-r--r-- 1 kbs 118 Dec 31 11:43 out.class
% javap -c -p out
public class out extends java.lang.Object {
public out();
Method out()
0 aload_0
1 invokenonvirtual #7 <Method java.lang.Object.<init>()V>
4 return
}
% javap -verify out
Class out succeeds
You should always run the verifier after generating bytecode. By
default, the verifier is not run when loading files locally, so
errors in the bytecode will cause the VM to die.
| |
|
T o o l s/Assembler/Script examples Simple | Loops | Mini arithmetic compiler
http://www.sbktech.org/jas-scm-simple.html
Revised: Thu Dec 19 08:51:14 1996 |
|